package qsoft.omanga.base.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import qsoft.omanga.business.entities.Chapter;
import qsoft.omanga.common.utils.DataConfig;

/**
 * 
 * @author sontt
 * 
 */
public class ChapterDAO {
    /**
     * 
     */
    private DBHelper dbHelper;

    /**
     * @param context
     *            Context
     */
    public ChapterDAO(Context context) {
        if (dbHelper == null) {
            dbHelper = new DBHelper(context);
        }
    }

    /**
     * @param chapterManagaId
     *            int
     * @return boolean
     */
    private boolean existChapter(int chapterManagaId) {
        boolean bResult = false;
        String where = DataConfig.CHAPTER_MANAGA_ID + " = " + chapterManagaId;
        Cursor cursor = dbHelper.getCondition(DataConfig.TABLE_CHAPTER,
                new String[] { DataConfig.CHAPTER_MANAGA_ID }, where,
                DataConfig.CHAPTER_MANAGA_ID);
        if (cursor != null && cursor.getCount() > 0) {
            bResult = true;
        }
        return bResult;
    }

    /**
     * @param chapter
     *            Chapter
     * @return long
     */
    public long insert(Chapter chapter) {
        long result = -1;
        if (existChapter(chapter.getManagaId())) {
            result = 1;
        } else {
            dbHelper.open();
            ContentValues contentValues = new ContentValues();
            contentValues.put(DataConfig.CHAPTER_MANAGA_ID,
                    chapter.getManagaId());
            contentValues.put(DataConfig.CHAPTER_NAME, chapter.getName());
            contentValues.put(DataConfig.CHAPTER_PAGE, chapter.getPage());
            result = dbHelper.insert(DataConfig.CREATE_TABLE_CHAPTER,
                    contentValues);
            dbHelper.close();
        }
        return result;
    }

    /**
     * @param mangaId
     *            String
     * @return ArrayList<Chapter>
     */
    public List<Chapter> getChapters(String mangaId) {
        List<Chapter> arrayList = new ArrayList<Chapter>();
        String where = DataConfig.CHAPTER_MANAGA_ID + " = " + mangaId;
        dbHelper.open();
        Cursor cursor = dbHelper.getCondition(DataConfig.TABLE_CHAPTER,
                new String[] { DataConfig.ALL }, where, DataConfig.CHAPTER_ID);
        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();            
            while (!cursor.isAfterLast()) {
            	Chapter chapter = new Chapter();
                chapter.setId(cursor.getInt(cursor
                        .getColumnIndex(DataConfig.ID)));
                chapter.setChapterid(cursor.getInt(cursor
                        .getColumnIndex(DataConfig.CHAPTER_ID)));
                chapter.setManagaId(cursor.getInt(cursor
                        .getColumnIndex(DataConfig.CHAPTER_MANAGA_ID)));
                chapter.setName(cursor.getString(cursor
                        .getColumnIndex(DataConfig.CHAPTER_NAME)));
                chapter.setPage(cursor.getInt(cursor
                        .getColumnIndex(DataConfig.CHAPTER_PAGE)));
                arrayList.add(chapter);
                cursor.moveToNext();
            }
        }

        if (cursor != null) {
            cursor.close();
        }
        dbHelper.close();
        return arrayList;
    }

}
